// 生成链表
class LinkNode {
    constructor(val, next) {
        this.val = val;
        this.next = next;
    }
}
// 确定是否为环形列表
var detectCycle = function (head) {
    if (!head || !head.next) return null;
    let slow = head.next, fast = head.next.next;
    while (fast && fast.next) {
        slow = slow.next;
        fast = fast.next.next;
        if (fast == slow) {
            slow = head;
            while (fast !== slow) {
                slow = slow.next;
                fast = fast.next;
            }
            return slow;
        }
    }
    return null;
};
// 先确定环形列表入口
var entrance = function (head) {
    if (!head || !head.next)
        console.log("null ");
    let slow = head.next;
    let fast = head.next.next;
    let extr1;
    let index = head;
    // 存放入口
    let enter;
    if (fast && fast.next) {
        fast = fast.next.next;
        slow = slow.next;
        // 确认存在环之后找入口
        if (fast == slow) {
            extr1 = slow;
            while (index !== extr1) {
                index = index.next;
                extr1 = extr1.next;
            }
            console.log(extr1);
        }
        enter = extr1;
    } else {
        console.log("null");
    }
}
// 删除
var removeElements = function (head, val) {
    const ret = new LinkNode(0, head);
    let cur = ret;
    while (cur.next) {
        if (cur.next.val == val) {
            cur.next = cur.next.next;
            continue;
        }
        cur = cur.next;
        // cur=cur.next实际是将cur引用的位置从当前节点移动到下一节点，
    }
    return ret.next;
}
// 增加
var addElements = function (newVal, val) {
    let newNode = new LinkNode(newVal);
    let curNode = this.head;    //找到要添加的节点
    let code = head;
    while (curNode.next) {
        curNode = curNode.next;
        if(code.next!==enter) {
            newcode.next = code.next;
            code.next = newcode;
        }
        // 添加在尾部
        else{
            code.next = newcode;
            newcode.next = enter;
        }
    }
    curNode.next = newNode;
}
// 查找
var findElements = function (val) {
    let curNode = this.head;    //从头节点开始遍历
    while (curNode && curNode.val) {
        if (curNode.val == val) {
            return curNode;
        }
        curNode = curNode.next;
        while(curNode = enter){
            console.log("Can't find val!!!");
        }
    }
    return null;

}
// 修改
var alterElements = function (NewVal, val) {
    let curNode = this.find(val);    //找到要修改的节点
    if (curNode) {
        curNode.val = newVal;
    }
    else {
        console.log("链表中不存在" + val + "元素");
    }
}